<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <script type="text/javascript" src="/Js/jquery.js"></script>
    <script type="text/javascript" src="../Js/bootstrap1.js"></script>
    <link rel="stylesheet" type="text/css" href="/Css/bootstrap1.css" />
    <link rel="stylesheet" type="text/css" href="/Css/style1.css" />

    <link href="/Css/fileinput.css" media="all" rel="stylesheet" type="text/css" />
    <script src="/Js/fileinput.js" type="text/javascript"></script>
    <script src="/Js/fileinput_locale_zh.js" type="text/javascript"></script>
</head>

<body>
<h2>更新药品图片</h2>
<form id="myForm" class="form-horizontal" action="/drug/" method="post">
    <div class="form-group">
        <input id="id" name="id" type="hidden">
        <label class="col-sm-1 control-label">药品名称:</label>
        <div class="col-sm-9">
            <input id="drugName" name="drugName" type="text" disabled/>
        </div>
    </div>
    <div class="form-group">
        <input id="oldImgName" name="oldImgName" type="text"/>
        <label class="col-sm-1 control-label">现有药品logo:</label>
        <div class="col-sm-9">
            <img id="drugLogo" name="drugLogo" src="/Images/"/>
        </div>
    </div>
    <div class="form-group">
        <label class="col-sm-1 control-label">选择药品新logo</label>
        <div class="col-sm-3">
            <input id="image" name="image"  type="file" required/>
        </div>
    </div>
    <div>
        <center>
            <button onclick="update()" class="btn btn-primary" type="button">保存</button> &nbsp;&nbsp;
            <button onclick="black()" class="btn btn-success" type="button">返回列表</button>
        </center>
    </div>
</form>
</body>
</html>
<script>
    $(function () {
        show();
    })
    function show(){
        //获取url
        (function($){
            $.getUrlParam
                = function(name)
            {
                var reg
                    = new RegExp("(^|&)"+
                    name +"=([^&]*)(&|$)");
                var r
                    = window.location.search.substr(1).match(reg);
                if (r!=null) return unescape(r[2]); return null;
            }
        })(jQuery);
        var id = $.getUrlParam('id');
        $.ajax({
            type:"GET",
            url: "/drug/getDrupById?id="+id,
            success: function(vo){
                $("#id").val(vo.id);
                $("#oldImgName").val(vo.img);
                $("#drugName").val(vo.drugName);
                $("#drugLogo").attr("src",vo.img);
            }
        });
    }
    function black() {
        window.location.href="/Medicine/index.html";
    }
    function update(){
        if($(image).val()=="" || $(image).val()==null){
            alert("上传图片为空");
            return false;
        }
        var form=$("#myForm");
        var formData=new FormData(form[0]);
        $.ajax({
            type:"POST",
            url:"/drug/updateLogo",
            data:formData,
            dataType:"json",
            contentType: false,
            processData: false,
            success: function(vo){
                alert(vo.msg);
                if(vo.code==200) {
                    black();
                }
            }
        });
    }
</script>